package com.ruoyi.record.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.record.domain.RecordLeave;
import com.ruoyi.record.domain.RecordSocietyContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.record.mapper.RecordSocietyMapper;
import com.ruoyi.record.domain.RecordSociety;
import com.ruoyi.record.service.IRecordSocietyService;
import com.ruoyi.common.core.text.Convert;

/**
 * 申请记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-23
 */
@Service
public class RecordSocietyServiceImpl extends ServiceImpl<RecordSocietyMapper, RecordSociety> implements IRecordSocietyService
{
    @Autowired
    private RecordSocietyMapper recordSocietyMapper;

    /**
     * 查询申请记录
     * 
     * @param societyId 申请记录主键
     * @return 申请记录
     */
    @Override
    public RecordSociety selectRecordSocietyBySocietyId(Long societyId)
    {
        return baseMapper.selectRecordSocietyBySocietyId(societyId);
    }

    /**
     * 查询申请记录列表
     * 
     * @param recordSociety 申请记录
     * @return 申请记录
     */
    @Override
    @DataScope(deptAlias = "s", userAlias = "u")
    public List<RecordSociety> selectRecordSocietyList(RecordSociety recordSociety)
    {
        return baseMapper.selectRecordSocietyList(recordSociety);
    }

    /**
     * 新增申请记录
     * 
     * @param recordSociety 申请记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRecordSociety(RecordSociety recordSociety)
    {
        recordSociety.setCreateTime(DateUtils.getNowDate());
        int rows = baseMapper.insert(recordSociety);
        insertRecordSocietyContent(recordSociety);
        return rows;
    }

    /**
     * 修改申请记录
     * 
     * @param recordSociety 申请记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRecordSociety(RecordSociety recordSociety)
    {
        recordSociety.setUpdateTime(DateUtils.getNowDate());
        recordSocietyMapper.deleteRecordSocietyContentBySocietyId(recordSociety.getSocietyId());
        insertRecordSocietyContent(recordSociety);
        return baseMapper.updateById(recordSociety);
    }

    /**
     * 修改申请记录
     *
     * @param recordSociety 申请记录
     * @return 结果
     */
    @Override
    public int verifyRecordSociety(RecordSociety recordSociety)
    {
        recordSociety.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(recordSociety);
    }

    /**
     * 查询待审批申请列表
     *
     * @param recordSociety 申请记录
     * @return 申请记录
     */
    @Override
    public List<RecordSociety> selectVerifySocietyList(RecordSociety recordSociety)
    {
        return baseMapper.selectVerifySocietyList(recordSociety);
    }

    /**
     * 批量删除申请记录
     * 
     * @param societyIds 需要删除的申请记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordSocietyBySocietyIds(String societyIds)
    {
        recordSocietyMapper.deleteRecordSocietyContentBySocietyIds(Convert.toStrArray(societyIds));
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(societyIds)));
    }

    /**
     * 删除申请记录信息
     * 
     * @param societyId 申请记录主键
     * @return 结果
     */
    @Override
    public int deleteRecordSocietyBySocietyId(Long societyId)
    {
        recordSocietyMapper.deleteRecordSocietyContentBySocietyId(societyId);
        return baseMapper.deleteById(societyId);
    }

    /**
     * 新增申请内容信息
     * 
     * @param recordSociety 申请记录对象
     */
    public void insertRecordSocietyContent(RecordSociety recordSociety)
    {
        List<RecordSocietyContent> recordSocietyContentList = recordSociety.getRecordSocietyContentList();
        Long societyId = recordSociety.getSocietyId();
        if (StringUtils.isNotNull(recordSocietyContentList))
        {
            List<RecordSocietyContent> list = new ArrayList<RecordSocietyContent>();
            for (RecordSocietyContent recordSocietyContent : recordSocietyContentList)
            {
                recordSocietyContent.setSocietyId(societyId);
                list.add(recordSocietyContent);
            }
            if (list.size() > 0)
            {
                recordSocietyMapper.batchRecordSocietyContent(list);
            }
        }
    }

    /**
     * 打印查询
     *
     * @param societyId 请假记录主键
     * @return 请假记录
     */
    @Override
    public RecordSociety selectRecordSocietyByPrint(Long societyId)
    {
        return baseMapper.selectRecordSocietyByPrint(societyId);
    }
}
